package com.huixue.cn.ztej.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huixue.cn.ztej.bean.Examine;
import com.huixue.cn.ztej.bean.MechanicalApply;
import com.huixue.cn.ztej.bean.Project;
import com.huixue.cn.ztej.bean.Results;
import com.huixue.cn.ztej.bean.User;
import com.huixue.cn.ztej.dao.EmployHourDao;
import com.huixue.cn.ztej.dao.MechanicalApplyMapper;
import com.huixue.cn.ztej.dao.SysProjectMapper;
import com.huixue.cn.ztej.dao.UserMapper;
import com.huixue.cn.ztej.enums.RoleStatus;
import com.huixue.cn.ztej.utils.SystemContents.API_RETURN_STATUS;
import com.huixue.cn.ztej.utils.framwork.PageParameter;

/**
 * 用工申请
 * @author Administrator
 *
 */

@Service
public class SysMechanicaApplyService {

	@Autowired
	MechanicalApplyMapper mechanicalApplyMapper;
	
	@Autowired
	private SysProjectMapper sysProjectMapper;
	
	@Autowired
	private UserMapper userMapper;

	
	public Results saveMechanicaApply(MechanicalApply model){
		
		long count = mechanicalApplyMapper.countResult(model);
		
		Project project = sysProjectMapper.findByProId(model.getProId());
		if(count>0){
			String newVisaCode = project.getProCode()+"-"+model.getConstructionName()+"-"+count;
			model.setVisaCode(newVisaCode);
		}else{
			String newVisaCode = project.getProCode()+"-"+model.getConstructionName()+"-"+1;
			model.setVisaCode(newVisaCode);
		}
		mechanicalApplyMapper.saveMechanicalApply(model);
		Results results = new Results(
				API_RETURN_STATUS.NORMAL.value(),
				API_RETURN_STATUS.NORMAL.desc());
		return results;
	}
	
	/**
	 * 查找所有申请
	 * @param map
	 * @return
	 */
	public Results listMechanicaApply(Map<String,Object> map){
		
		Results results = new Results(
				API_RETURN_STATUS.NORMAL.value(),
				API_RETURN_STATUS.NORMAL.desc());
		
		List<MechanicalApply> list = mechanicalApplyMapper.listMechanicalApply(map);
		
		results.setData(list);
		
		return results;
	}
	
	/**
	 * 查找所有机械申请
	 * @param map
	 * @return
	 */
	public Results searchMechanicalApplyPage(Map<String,Object> map,Integer pageNumber, Integer pageSize){
		
		Map<String, Object> rmap = new HashMap<String, Object>();
		PageParameter page = new PageParameter();
		page.setCurrentPage(pageNumber);
		page.setPageSize(pageSize);
		map.put("page", page);
		
		Results results = new Results(
				API_RETURN_STATUS.NORMAL.value(),
				API_RETURN_STATUS.NORMAL.desc());
		
		List<MechanicalApply> list = mechanicalApplyMapper.searchMechanicalApplyPage(map);
		if(list!=null && list.size()>0){
			Map<String,Object>  searchParams = new HashMap<>();

			
			for(MechanicalApply mechApply: list){
				searchParams.clear();
				searchParams.put("projectID", mechApply.getProId()+"");
				//mechApply.setApplyUser(userMapper.findById(emp.getApplyUserId()));
				mechApply.setUser(userMapper.findById(mechApply.getApplyUserId()));
				if(mechApply.getState()==0){
					searchParams.put("segmentID", mechApply.getSegmentId()+"");
					searchParams.put("roleId", RoleStatus.ROLE_4.getRoleId()+"");
					List<User> userList = userMapper.findUserByParams(searchParams);
					if(userList!=null && userList.size()>0){
						mechApply.setNextUser(userList.get(0));
					}
				}else if(mechApply.getState()==4){
					//searchParams.put("segmentID", mechApply.getSegmentId()+"");
					searchParams.put("roleId", RoleStatus.ROLE_3.getRoleId()+"");
					List<User> userList = userMapper.findUserByParams(searchParams);
					//System.out.println("==mechApply.getState()=="+mechApply.getState());
					if(userList!=null && userList.size()>0){
						mechApply.setNextUser(userList.get(0));
					}
					//System.out.println("==userList=="+userList.size());
				}else if(mechApply.getState()==3){
					searchParams.put("roleId", RoleStatus.ROLE_5.getRoleId()+"");
					List<User> userList = userMapper.findUserByParams(searchParams);
					if(userList!=null && userList.size()>0){
						mechApply.setNextUser(userList.get(0));
					}
				}else if(mechApply.getState()==5){
					searchParams.put("roleId", RoleStatus.ROLE_6.getRoleId()+"");
					List<User> userList = userMapper.findUserByParams(searchParams);
					if(userList!=null && userList.size()>0){
						mechApply.setNextUser(userList.get(0));
					}
				}else{
					mechApply.setNextUser(new User());
				}
			}
		}
		rmap.put("total", page.getTotalPage());
		rmap.put("page", pageNumber);						//当前页
		rmap.put("records", page.getTotalCount());			//总记录数
		rmap.put("pageSize", pageSize);						//每页个数
		rmap.put("rows", list);
		
		results.setData(rmap);
		
		return results;
	}
	
	
	/**
	 * 查找申请
	 * @param applyId
	 * @return
	 */
	public MechanicalApply findById(int applyId){
		return mechanicalApplyMapper.findById(applyId);
	}
	
	/**
	 * 更新申请
	 * @param applyId
	 */
	public void update(MechanicalApply model){
		 mechanicalApplyMapper.update(model);
	}
	
	/**
	 * 根据用户查找申请
	 * @param applyUserId
	 * @return
	 */
	public Results  listMechanicalApplyByUserId(int applyUserId){
		Results results = new Results(
				API_RETURN_STATUS.NORMAL.value(),
				API_RETURN_STATUS.NORMAL.desc());
		
		List<MechanicalApply> list = mechanicalApplyMapper.listMechanicalApplyByUserId(applyUserId);
		
		results.setData(list);
		
		System.out.println("=机械申请=list=="+list.size());
		return results;
	}
	
}
